﻿
using EBA.IoC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace EBA.Framework
{
    /// <summary>
    /// 插件分析器
    /// </summary>
    [Serializable]
    public class BundleAnalyzer : MarshalByRefObject
    {
        /// <summary>
        /// 载入指定目录底下的所有dll文件，并提取插件列表
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public IEnumerable<BundleManifest> GetBundleManifests<TBundle>(string folder) where TBundle:IBundle
        {
            var bin = new DirectoryInfo(folder);

            var files = bin.GetFiles().Select(i => i.FullName).ToList();

            var container = new ContainerConfiguration()
                .WithAssemblies(WindowsAssembly.GetAssemblies(folder, "*.dll"))
                .CreateContainer();

            foreach (var bundle in container.GetAllExports<TBundle>())
            {
                yield return new BundleManifest(bundle) { Files = files };
            }

            yield break;
        }
    }
}
